/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pos.control;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import pos.Pos;
import pos.entity.Item;
import pos.storage.db.sql.SQL;
import pos.util.IDGenerator;

/**
 * This is the control object class that takes input from AddItem bound object
 * @author rsrikanth
 */
public class AddItemUPCControl {

    /**
     * Adds a new Item with UPC code into the repository
     * @param item
     * @return 
     */
    public boolean add(Item item, String categoryId){
        try{
            IDGenerator newItemId = new IDGenerator();
            IDGenerator newCateItemId = new IDGenerator();
            
            long itemId = newItemId.getID();
            long categoryItemId = newCateItemId.getID();

            PreparedStatement stmt = Pos.adminConn.prepareStatement(SQL.INSERTITEM);
            PreparedStatement stmt2 = Pos.adminConn.prepareStatement(SQL.INSERT_CATEGORYITEM);

            stmt.setLong(1,itemId);
            stmt.setString(2,item.getName());
            stmt.setString(3,item.getDesc());
            stmt.setString(4,item.getUPCode());
            stmt.setDouble(5,item.getBasePrice());
            stmt.setDate(6, item.getDateTimeOfManufacture());
            stmt.setDate(7, item.getExpiryDateTime());
            stmt.setDate(8, new Date(System.currentTimeMillis()));
            stmt.setDate(9, new Date(System.currentTimeMillis()));

            stmt2.setLong(1, categoryItemId);
            stmt2.setString(2,categoryId);
            stmt2.setLong(3,itemId);
            stmt2.setDate(4,new Date(System.currentTimeMillis()));
            stmt2.setDate(5,new Date(System.currentTimeMillis()));

            stmt.executeUpdate();
            stmt2.executeUpdate();
            return true;
        }
        catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
}
